package cn.haut.online_read.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author xing
 * @date 2021-05-20 0:16
 */
@Component
@Slf4j
public class RedisUtils {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 设置 key, value
     *
     * @param key
     * @param value
     */
    public void set(String key, String value) {
//        log.error("用户名:{}", key);
//        log.error("token:{}", value);
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value);
    }

    /**
     * 设置 key, value, 过期时间
     *
     * @param key
     * @param value
     * @param seconds
     */
    public void setex(String key, String value, int seconds) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, value, seconds);
    }

    /**
     * 根据 key 获取 值
     * @param key
     * @return
     */
    public String getValue(String key) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }

    /**
     * 根据 key 删除 数据
     *
     * @param key
     */
    public Boolean deleteValue(String key) {
        return redisTemplate.delete(key);
    }
}
